//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "FdmBlackScholesSolver.h"
using namespace Cephei::QL::Experimental::Finitedifferences;
#include <gen/QL/Processes/GeneralizedBlackScholesProcess.h>
#include <gen/QL/Experimental/Finitedifferences/FdmMesher.h>
#include <gen/QL/Experimental/Finitedifferences/FdmStepConditionComposite.h>
#include <gen/QL/Experimental/Finitedifferences/FdmInnerValueCalculator.h>
using namespace Cephei::QL::Processes;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Finitedifferences::CFdmBlackScholesSolver::CFdmBlackScholesSolver (boost::shared_ptr<QuantLib::FdmBlackScholesSolver>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmBlackScholesSolver = NULL;
#endif
	_ppFdmBlackScholesSolver = &childNative;
    
}
Cephei::QL::Experimental::Finitedifferences::CFdmBlackScholesSolver::CFdmBlackScholesSolver (QuantLib::FdmBlackScholesSolver& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phFdmBlackScholesSolver = NULL;
#endif
	_ppFdmBlackScholesSolver = new boost::shared_ptr<QuantLib::FdmBlackScholesSolver> (&childNative);
    
    _FdmBlackScholesSolverOwner = owner;
    
}

Cephei::QL::Experimental::Finitedifferences::CFdmBlackScholesSolver::CFdmBlackScholesSolver (CFdmBlackScholesSolver^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmBlackScholesSolver = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFdmBlackScholesSolver = new boost::shared_ptr<QuantLib::FdmBlackScholesSolver> (copy->GetShared());
        
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdmBlackScholesSolver::CFdmBlackScholesSolver (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmBlackScholesSolver = NULL;
#endif
	if (!t->IsSubclassOf(CFdmBlackScholesSolver::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Finitedifferences::CFdmBlackScholesSolver::CFdmBlackScholesSolver (QuantLib::Handle<QuantLib::FdmBlackScholesSolver>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFdmBlackScholesSolver = &childNative;
	_ppFdmBlackScholesSolver = &static_cast<boost::shared_ptr<QuantLib::FdmBlackScholesSolver>>(childNative.currentLink());
    
    _FdmBlackScholesSolverOwner = owner;
}
Cephei::QL::Experimental::Finitedifferences::CFdmBlackScholesSolver::CFdmBlackScholesSolver (QuantLib::Handle<QuantLib::FdmBlackScholesSolver> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFdmBlackScholesSolver = &childNative;
	_ppFdmBlackScholesSolver = &static_cast<boost::shared_ptr<QuantLib::FdmBlackScholesSolver>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Finitedifferences::CFdmBlackScholesSolver::CFdmBlackScholesSolver (QuantLib::FdmBlackScholesSolver childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmBlackScholesSolver = NULL;
#endif
	_ppFdmBlackScholesSolver = new boost::shared_ptr<QuantLib::FdmBlackScholesSolver> (new QuantLib::FdmBlackScholesSolver (childNative));
    
}
#endif

Cephei::QL::Experimental::Finitedifferences::CFdmBlackScholesSolver::~CFdmBlackScholesSolver ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppFdmBlackScholesSolver != NULL)
    {
	    delete _ppFdmBlackScholesSolver;
        _ppFdmBlackScholesSolver = NULL;
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdmBlackScholesSolver::!CFdmBlackScholesSolver ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppFdmBlackScholesSolver != NULL)
    {
	    delete _ppFdmBlackScholesSolver;
    }
}
QuantLib::FdmBlackScholesSolver& Cephei::QL::Experimental::Finitedifferences::CFdmBlackScholesSolver::GetReference ()
{
    if (_ppFdmBlackScholesSolver == NULL) throw gcnew NativeNullException ();
	return **_ppFdmBlackScholesSolver;
}
boost::shared_ptr<QuantLib::FdmBlackScholesSolver>& Cephei::QL::Experimental::Finitedifferences::CFdmBlackScholesSolver::GetShared ()
{
    if (_ppFdmBlackScholesSolver == NULL) throw gcnew NativeNullException ();
	return *_ppFdmBlackScholesSolver;
}
QuantLib::FdmBlackScholesSolver* Cephei::QL::Experimental::Finitedifferences::CFdmBlackScholesSolver::GetPointer ()
{
    if (_ppFdmBlackScholesSolver == NULL) throw gcnew NativeNullException ();
	return &**_ppFdmBlackScholesSolver;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FdmBlackScholesSolver>& Cephei::QL::Experimental::Finitedifferences::CFdmBlackScholesSolver::GetHandle ()
{
	if (_phFdmBlackScholesSolver == NULL)
	{
		_phFdmBlackScholesSolver = new Handle<QuantLib::FdmBlackScholesSolver> (*_ppFdmBlackScholesSolver);
	}
	return *_phFdmBlackScholesSolver;
}
#endif
bool Cephei::QL::Experimental::Finitedifferences::CFdmBlackScholesSolver::HasNative () 
{
	return (_ppFdmBlackScholesSolver != NULL);
}

Double Cephei::QL::Experimental::Finitedifferences::CFdmBlackScholesSolver::DeltaAt (Double s)
{
    try
    {
        QuantLib::Real _s = (QuantLib::Real)ValueHelper::Convert (s);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFdmBlackScholesSolver)->deltaAt ( _s );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Finitedifferences::CFdmBlackScholesSolver::GammaAt (Double s)
{
    try
    {
        QuantLib::Real _s = (QuantLib::Real)ValueHelper::Convert (s);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFdmBlackScholesSolver)->gammaAt ( _s );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Finitedifferences::CFdmBlackScholesSolver::ThetaAt (Double s)
{
    try
    {
        QuantLib::Real _s = (QuantLib::Real)ValueHelper::Convert (s);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFdmBlackScholesSolver)->thetaAt ( _s );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Finitedifferences::CFdmBlackScholesSolver::ValueAt (Double s)
{
    try
    {
        QuantLib::Real _s = (QuantLib::Real)ValueHelper::Convert (s);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFdmBlackScholesSolver)->valueAt ( _s );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

